﻿using HandyControl.Controls;
using YY.Admin.Core.BusinessException;

namespace YY.Admin.EventBus
{
    // 错误事件
    public class ErrorEvent : PubSubEvent<string>
    {
    }

    // 错误处理服务
    public interface IErrorHandler
    {
        void HandleError(Exception ex);
    }

    public class ErrorHandler : IErrorHandler
    {
        private readonly IEventAggregator _eventAggregator;

        public ErrorHandler(
            IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        public void HandleError(Exception ex)
        {
            if (ex is BusinessException bex)
            {
               // _logger.LogWarning($"业务错误: {bex.ErrorCode} - {bex.Message}");

                // 在UI线程显示HandyControl提示
                System.Windows.Application.Current.Dispatcher.Invoke(() =>
                {
                    Growl.Error(bex.Message);
                });
            }
            else
            {
               // _logger.LogError(ex, "未处理的异常");
                _eventAggregator.GetEvent<ErrorEvent>().Publish($"系统错误: {ex.Message}");
            }
        }
    }
}
